#!/bin/sh
# watch.sh - execute a program periodically, showing output
# Copyright (c) 2009-2011 Rudá Moura <ruda.moura@gmail.com>

TITLE=true
INTERVAL=1
USAGE="Usage: $0 [-ht] [-n <seconds>] <command>"

while getopts "htn:" OPTION ; do
    case $OPTION in
	h) echo $USAGE
	    exit 0
	    ;;
	t) TITLE=false
	    ;;
	n) INTERVAL=$OPTARG
	    ;;
	?) exit 1
	    ;;
    esac
done

shift $(($OPTIND - 1))
if [ $# == 0 ]; then
    echo $USAGE
    exit 0
fi

while true; do
    clear
    NOW=$(date)
    $TITLE && echo "Every ${INTERVAL}s: ${*}	$NOW" && echo
    "$@"
    sleep $INTERVAL
done
